package show;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.multimedia.jolitorask.security.AuthServlet;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocal;

public class ShowUsers extends AuthServlet implements javax.servlet.Servlet {

	private static final long serialVersionUID = 1L;	
	
	public static int mode;
	public static int LOGIN = 1;
	public static int NAME_AND_FIRSTNAME = 2;
	public static int ALL = 0;
    private String account, login, name, firstname;
	
	private void syntax_error(PrintWriter out){
		
		JSONObject error = new JSONObject();
		
		try {
			error.put("account", account);
			error.put("size", 0);
			error.put("users", new JSONArray());
			out.println(error.toString());
		} catch (JSONException e) {
			out.println(""+null);
		}
		
	}
	
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

		
		mode = ALL;
		
	    res.setContentType("text/plain");
	    PrintWriter out = res.getWriter();
	    
	    /* On recupere le password */
	    if( (account=req.getParameter("account")) == null){
	    	syntax_error(out);
	    	return;
	    }
	    
	    
	    login = req.getParameter("login");
	    name = req.getParameter("name");
	    firstname =req.getParameter("firstname");
	    
	    if( login != null ){
	    	
	    	if (name != null || firstname != null) {
	    		syntax_error(out);
	    		return;
	    	}
	    	mode = LOGIN;

	    }
	    else {

	    	if (name != null || firstname != null) {
	    	
		    	if( name == null)
		    		name = "";
		    	if( firstname == null)
		    		firstname = "";
	    	
		    	mode = NAME_AND_FIRSTNAME;
	    	}
	    }
	    
	    
	    if( !authentificate(req)){
	    	out.println(false);
	    	return;
	    }
	    	

	    try {
	    	
    		Iterator<UtilisateurLocal> iteUtilisateur = client.getUtilisateurs().iterator();
    		UtilisateurLocal u;
    		int i = 0;
    		JSONObject json = new JSONObject();
    		JSONArray users = new JSONArray();
    		JSONArray array;
    		JSONArray names = new JSONArray();
    		names.put("login");
    		names.put("name");
    		names.put("firstname");
    		
	    	if(mode == ALL){

	    		while(iteUtilisateur.hasNext()){
	    			
		    		u = iteUtilisateur.next();
		    		
		    		array = new JSONArray();
		    		array.put(u.getLogin());
		    		array.put(u.getNom());
		    		array.put(u.getPrenom());
		    		users.put(array.toJSONObject(names));
	    			i++;
		    	}
	    	}
	    	else if( mode == LOGIN ){
	    		
	    		while(iteUtilisateur.hasNext()){

		    		u = iteUtilisateur.next();
		    		if( u.getLogin().equals(login) ){
		    			array = new JSONArray();
		    			array.put(u.getLogin());
		    			array.put(u.getNom());
			    		array.put(u.getPrenom());
			    		users.put(array.toJSONObject(names));
		    			i++;
	    			}
		    	}
	    	}
	    	else if( mode == NAME_AND_FIRSTNAME ){
	    		
	    		while(iteUtilisateur.hasNext()){
	    			
	    			u = iteUtilisateur.next();
	    			if( u.getNom().contains(name) && u.getPrenom().contains(firstname) ){
	    				array = new JSONArray();
	    				array.put(u.getLogin());
	    				array.put(u.getNom());
			    		array.put(u.getPrenom());
			    		users.put(array.toJSONObject(names));
		    			i++;
	    			}
	    		}
	    	}
	    	
	    	json.put("account", account);
	    	json.put("size", i);
    		json.put("users", users);
    		out.println(json.toString());
    		
	    }
	    catch(Exception e){
	    	syntax_error(out);
	    	return;
	    }
}  	

}